package com.block.common.util;

import cn.hutool.core.util.StrUtil;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.constant.SmsConstant;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class SmsCodeUtil {

    private static Logger log= LogFactory.getAppReq();

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public ResponseVo checkCode(String mobile, String operationType, String verifyCode){

        String redisKey = SmsConstant.getCodeKey(operationType, mobile);
        String cacheCode = stringRedisTemplate.opsForValue().get(redisKey);
        log.info("【验证码方式更新手机号】验证信息【redisKey】{}【mobile】{}【verifyCode】{}【cacheVerifyCode】{}",
                redisKey, mobile, verifyCode, cacheCode);
        if (StrUtil.isEmpty(cacheCode)){
            return new ResponseVo(AppRspCodeConstant.VERIFY_CODE_ERROR);
        }

        if (!cacheCode.equals(verifyCode)){
            return new ResponseVo(AppRspCodeConstant.VERIFY_CODE_ERROR);
        }

        // 删除验证码缓存，并返回验证正确
        stringRedisTemplate.delete(redisKey);
        return new ResponseVo(AppRspCodeConstant.SUCCESS);
    }

}
